package main

import (
	. "bsb"
	"flag"
	"fmt"
)

type fInter func(interface{}, Compare) int
type fInt func(int, Compare) int
type fIntArr func(int, IntArray) int

var s = flag.Int("s", 1, "search benchmark")
var N = flag.Int("N", 10000000, "number of iterations")

func makeArray() IntArray {
	b := IntArray(make([]int, *N))
	for i := 0; i < len(b); i++ {
		b[i] = i
	}
	return b
}

func check(key, index, want int) {
	if index != want {
		fmt.Printf("Bsearch(%d,b) = %d; want %d", key, index, want)
	}
}

func (search fInter) Bench() {
	b := makeArray()
	for key := -1; key < len(b); key++ {
		check(key, search(key, b), key)
	}
	key := len(b) + 1
	check(key, search(key, b), -1)
}

func (search fInt) Bench() {
	b := makeArray()
	for key := -1; key < len(b); key++ {
		check(key, search(key, b), key)
	}
	key := len(b) + 1
	check(key, search(key, b), -1)
}

func (search fIntArr) Bench() {
	b := makeArray()
	for key := -1; key < len(b); key++ {
		check(key, search(key, b), key)
	}
	key := len(b) + 1
	check(key, search(key, b), -1)
}

func main() {
	flag.Parse()

	switch *s {
	case 1:
		fInter(TAssertLtGt).Bench()
	case 2:
		fInter(TAssertCmp).Bench()
	case 3:
		fInt(IntLtGt).Bench()
	case 4:
		fInt(IntCmp).Bench()
	case 5:
		fIntArr(Int1).Bench()
	case 6:
		fIntArr(Int2).Bench()
	case 7:
		fIntArr(Int3).Bench()
	case 8:
		fIntArr(Int4).Bench()
	}
}
